package com.example.interviewassistant;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_CODE_RESUME = 1001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnUploadResume = findViewById(R.id.btnUploadResume);
        Button btnConfigureAPI = findViewById(R.id.btnConfigureAPI);

        btnUploadResume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ApiConfigManager.hasValidConfig(MainActivity.this)) {
                    ResumeUploader.selectResume(MainActivity.this, REQUEST_CODE_RESUME);
                } else {
                    Toast.makeText(MainActivity.this, "请先配置API设置", Toast.LENGTH_SHORT).show();
                    // 自动跳转到API配置界面
                    Intent intent = new Intent(MainActivity.this, ApiConfigActivity.class);
                    startActivity(intent);
                }
            }
        });

        btnConfigureAPI.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ApiConfigActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        
        if (requestCode == REQUEST_CODE_RESUME && resultCode == RESULT_OK && data != null) {
            Uri resumeUri = data.getData();
            if (resumeUri != null) {
                handleSelectedResume(resumeUri);
            } else {
                Toast.makeText(this, "无法获取所选文件", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void handleSelectedResume(Uri resumeUri) {
        try {
            // 解析简历内容
            String resumeContent = ResumeUploader.parseResumeContent(this, resumeUri);
            if (resumeContent == null || resumeContent.isEmpty()) {
                Toast.makeText(this, "无法读取简历内容", Toast.LENGTH_SHORT).show();
                return;
            }

            // 先进入问题配置界面，再生成面试问题
            Intent intent = new Intent(this, QuestionConfigActivity.class);
            intent.putExtra(QuestionConfigActivity.EXTRA_RESUME_TEXT, resumeContent);
            startActivity(intent);
        } catch (Exception e) {
            Toast.makeText(this, "解析简历失败: " + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
} 